package com.zy.controller;

import cn.hutool.json.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.protocol.x.Notice;
import com.zy.common.DataGridView;
import com.zy.common.ResultObject;
import com.zy.common.tree.TreeNode;
import com.zy.shiro.domain.SysMenu;
import com.zy.shiro.domain.SysRole;
import com.zy.shiro.domain.SysUser;
import com.zy.shiro.mapper.SysRoleMapper;
import com.zy.shiro.service.SysRoleService;
import com.zy.vo.RoleVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/sys/role")
public class RoleController {

  @Autowired
  private SysRoleService sysRoleService;

  @GetMapping("/loadAllRole")
  public DataGridView loadAllRole(RoleVo roleVo){
    if (roleVo==null){
      new DataGridView();
    }
    Page<SysRole> page =new Page<>(roleVo.getPage(),roleVo.getLimit());
//    page.setSize(roleVo.getLimit());
//    page.setPages(roleVo.getPage());
    IPage<SysRole> sysRoleIPage = sysRoleService.queryAllRole(page, roleVo);
//    List<SysRole> list = sysRoleService.list();
    DataGridView dataGridView = new DataGridView(sysRoleIPage.getTotal(),sysRoleIPage.getRecords());
    return dataGridView;
  }

  @GetMapping("/loadAllMenuByRoleId")
  public DataGridView loadAllMenuByRoleId(Integer roleId){
    if(null!=roleId){//如果activerUser为空说明当前访问者没有登陆
      List<SysMenu> menuList=null;
      menuList=this.sysRoleService.queryAllMenuByRoleId(roleId);
      List<TreeNode> treeNodes=new ArrayList<>();
      for (SysMenu m : menuList) {
//        Boolean spread=m.getSpread()==1?true:false;
//        treeNodes.add(new TreeNode(m.getId(),m.getPid(),m.getTitle(),m.getIcon(),m.getHref(),m.getTarget(),spread));
        treeNodes.add(new TreeNode(m.getMenuId().intValue(),m.getParentId().intValue(),m.getMenuName(),m.getIcon(),m.getUrl(),"_self",Boolean.TRUE));
      }
      DataGridView dataGridView = new DataGridView(TreeNode.TreeNodeBuilder.build(treeNodes, 0));
      return  dataGridView;
    }else{
      return new DataGridView(-1,null);
    }

  }

  @PostMapping("/updateRole")
  public ResultObject updateRole(SysRole sysRole){
    try {
//      sysRole.setCreateTime(new Date());
//      SysUser sysUser= (SysUser) SecurityUtils.getSubject();
//      sysRole.setCreateUserId((long) 1);

      sysRoleService.updateById(sysRole);
      return new ResultObject(1,"添加角色成功！");
    }catch (Exception e){
      return new ResultObject(-1,"添加角色失败！");
    }
  }

  /**
   * 添加角色
   */
  @PostMapping("/addRole")
  public ResultObject addRole(SysRole sysRole){
    try {
      sysRole.setCreateTime(new Date());
//      SysUser sysUser= (SysUser) SecurityUtils.getSubject();
      sysRole.setCreateUserId((long) 1);
      sysRoleService.save(sysRole);
      return new ResultObject(1,"添加角色成功！");
    }catch (Exception e){
      return new ResultObject(-1,"添加角色失败！");
    }
  }
  /**
   * 删除角色
   */
  @PostMapping("/deleteRole")
  public ResultObject deleteRole(Integer id){
    try {
//      Long roleId = sysRole.getRoleId();
      sysRoleService.removeById(id);
      return new ResultObject(1,"删除角色成功！");
    }catch (Exception e){
      return new ResultObject(-1,"删除角色失败！");
    }
  }
  /**
   * 批量删除
   */
  @PostMapping("/batchDeleteRole")
  public ResultObject batchDeleteRole(Integer[] ids){
    try {
      if(ids!=null&&ids.length>0){

        List<Integer> listIds=new ArrayList<>(ids.length);
        for (Integer id : ids) {
          listIds.add(id);
        }
        this.sysRoleService.removeByIds(listIds);
        return new ResultObject(1,"批量删除成功！");
      }else{
        return new ResultObject(-1,"批量删除失败，id数组不能为空！");
      }
    }catch (Exception e){
      return new ResultObject(-1,"批量删除失败！");
    }
  }



}
